package prueba;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Carro extends javax.swing.JFrame implements ActionListener{
    // Declaracion de variables
    private JButton botones[];
    private float estaciones[];
    private final String nombres[]={"1","2","3","4","5","6","7","8","9","10","11","12"};
    private int contador=0;
    private int contadorOn=0;
    private int contadorAM=0;
    private boolean encendido;
    private boolean signal;
    RadioAM RadioActual = new RadioAM();

    public Carro() {
                         
        initComponents();
        
        // Se crea arreglo de 12 botones para GUI
        botones = new JButton[12];
        Botones.setLayout( new GridLayout(4,3));
        for(int i=0;i<12;i++)
        {
                botones[i]= new JButton(nombres[contador]); 
                botones[i].addActionListener(this);
                Botones.add(botones[i]);                       
                contador++;
        }
        // se deshabilidta todos los botones al inicio del programa
        this.add(Botones,BorderLayout.SOUTH);
           emisora.setEnabled(false);
           botonGuardaEmisora.setEnabled(false);
           botonAMFM.setEnabled(false);
           Botones.setVisible(false);
           jTextField1.setEnabled(false);
        
    }
    // Metodo que controla los eventos con los botones
    public void actionPerformed(ActionEvent e)
    {
        for(int i=0;i<12;i++)
        {
          if(e.getSource().equals(botones[i]))
          {
              RadioActual.ChangeTune(i+1);
              cambiarEmisora();
          }
        }        
    }
    // Metodo utilizado para imprimir emisora en Boton
    public void cambiarEmisora()
    {
        emisora.setText(String.format("%.2f",RadioActual.GetFreq()));
        //emisora.setText(String.format("%.2f",String.valueOf(RadioActual.GetFreq())));
    }
    

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        botonOnOff = new javax.swing.JButton();
        Botones = new javax.swing.JPanel();
        botonAMFM = new javax.swing.JButton();
        emisora = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        botonGuardaEmisora = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("87.9/530");

        jLabel5.setText("FM/AM");

        jLabel4.setText("FM/AM");

        jLabel2.setText("107.9/1610");

        botonOnOff.setText("ON/OFF");
        botonOnOff.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonOnOffActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout BotonesLayout = new javax.swing.GroupLayout(Botones);
        Botones.setLayout(BotonesLayout);
        BotonesLayout.setHorizontalGroup(
            BotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        BotonesLayout.setVerticalGroup(
            BotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 116, Short.MAX_VALUE)
        );

        botonAMFM.setText("AM/FM");
        botonAMFM.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAMFMActionPerformed(evt);
            }
        });

        emisora.setText("530");
        emisora.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                emisoraActionPerformed(evt);
            }
        });

        botonGuardaEmisora.setText("Guardar");
        botonGuardaEmisora.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardaEmisoraActionPerformed(evt);
            }
        });

        jLabel3.setText("Guardar emisora  en  boton:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(67, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(Botones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(botonAMFM)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 171, Short.MAX_VALUE)
                        .addComponent(botonOnOff)
                        .addGap(13, 13, 13)))
                .addGap(38, 38, 38))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(98, 98, 98)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(39, 39, 39)
                        .addComponent(emisora, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(botonGuardaEmisora, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel5))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonOnOff)
                    .addComponent(botonAMFM))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1))
                    .addComponent(emisora, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(botonGuardaEmisora, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addComponent(Botones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Metodo utlizado para apagar o encender el radio        
    private void botonOnOffActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonOnOffActionPerformed
        // TODO add your handling code here:
        //si el radio esta encendido se habilitan botones, de lo contrario
        // los botones se deshabilitan
        contadorOn++;
        if((contadorOn%2)==1)
        {
           RadioActual.Power();           
           emisora.setEnabled(true);
           botonGuardaEmisora.setEnabled(true);
           botonAMFM.setEnabled(true);
           Botones.setVisible(true);
           jTextField1.setEnabled(true);
        } 
        else
        {            
          // Inhabilitacion de botones
           emisora.setEnabled(false);
           botonGuardaEmisora.setEnabled(false);
           botonAMFM.setEnabled(false);
           Botones.setVisible(false);
           jTextField1.setEnabled(false);

        }
           
    }//GEN-LAST:event_botonOnOffActionPerformed
// Metodo utlizado para cambiar de AM para FM
    private void botonAMFMActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAMFMActionPerformed
        // TODO add your handling code here:
        // se cambia de modo AM para FM y viceversa
        RadioActual.ChangeSignal();              
        // se imprime en boton la emisora
        emisora.setText(String.format("%.2f",RadioActual.GetFreq()));
        
    }//GEN-LAST:event_botonAMFMActionPerformed
  // boton que contiene la emisora actual
    private void emisoraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_emisoraActionPerformed
          
        emisora.setText(String.format("%.2f", RadioActual.NextFreq()));
    }//GEN-LAST:event_emisoraActionPerformed
// Metodo utilizado para guardar emisora del textfield
    private void botonGuardaEmisoraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardaEmisoraActionPerformed
       
        if(Integer.parseInt(jTextField1.getText())<13)
        {
              RadioActual.SaveFreq(Integer.parseInt(jTextField1.getText()));
           //   cambiarEmisora();                          
        }
        jTextField1.setText("");
        
    }//GEN-LAST:event_botonGuardaEmisoraActionPerformed

    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Carro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Carro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Carro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Carro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Carro().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel Botones;
    private javax.swing.JButton botonAMFM;
    private javax.swing.JButton botonGuardaEmisora;
    private javax.swing.JButton botonOnOff;
    private javax.swing.JButton emisora;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
